<?php

namespace App\Rpc;

use App\Common\ServiceHelper;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Rpc\Client\Client;
use Swoft\Rpc\Client\Contract\ProviderInterface;

/**
 * Class RpcProvider
 * @Bean()
 */
class RpcProvider implements ProviderInterface {

    protected $service_name;  //服务名bean_rpc.php->userProvider->service_name

    /**
     * @Inject()
     *
     * @var ServiceHelper
     */
    private $serviceHelper;

    /**
     * @param Client $client
     *
     * @return array
     *
     * @example
     * [
     *     'host:port',
     *     'host:port',
     *     'host:port',
     * ]
     */
    public function getList(Client $client): array
    {
        $services = $this->serviceHelper->getService($this->service_name);
        $ret = [];
        foreach ($services as $key=>$value) {
            $ret[$key] = $value["Address"] . ":" . $value["Port"];
        }
        return $ret;
    }
}